package fons.model.ontology.language;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class Sentence {
	private final String sentence;
	private final List<SentenceElement> elements = Lists.newArrayList();
	
	public Sentence (String sentence) {
		this.sentence = sentence;
	}
		
	public String getText() {
		return this.sentence;
	}
	
	public void setElements(Collection<? extends SentenceElement> elements) {
		this.elements.addAll(elements);
		Collections.sort(this.elements);
	}
	
	public Collection<? extends SentenceElement> getElements() {
		return Collections.unmodifiableList(elements);
	}
	
	public <T extends SentenceElement> Collection<T> getElementsOf (Class<T> clazz) {
		return Lists.newArrayList(Iterables.filter(elements, clazz));
	}
	
	public SentenceElement getNext (SentenceElement current) {
		int index = elements.indexOf(current);
		if (index != -1 && index + 1 < elements.size()) {
			return elements.get(index + 1);
		}
		return null;
	}

	@Override
	public String toString() {
		return "Sentence [sentence=" + sentence + "]";
	}

	public SentenceElement getPrevious(SentenceElement current) {
		int index = elements.indexOf(current);
		if (index != -1 && 0 <= index - 1) {
			return elements.get(index - 1);
		}
		return null;
	}

	public boolean remove(SentenceElement element) {
		return elements.remove(element);
	}

	public void insertAfter(SentenceElement position, SentenceElement addition) {
		int index = elements.indexOf(position);
		elements.add(index+1, addition);
	}

	public SentenceElement getElementAfter(Object reference) {
		for (int i = 0; i < elements.size(); ++i) {
			if (elements.get(i).get(Object.class) == reference) {
				return elements.get(i+1);
			}
		}
		return null;
	}
}
